﻿using System;
using Model;
using Service;


public partial class Own : System.Web.UI.Page
{

    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    EquipmentSystem _system = new EquipmentSystem();
    private DirectService _DirectService = new DirectService();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTotalOwner.Text = "Total owner:" + _DirectService.GetOwnDiplayed(GetWhereClause(), GetOrderClause()).Rows.Count;

        //  txtHiddenSystemId.Text = Request.QueryString["systemsId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;

        new Utility().SetRowToDisplay(grdOwn, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {
            // to avoid loading for the first time


            if (Session["pageIndex"] != null)
            {
                grdOwn.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }



            /* 
             * 1. number of equipments
             * 2. select the element 
             * */


        }
        else
        {
            txtHiddenPageIndex.Text = "0";
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            lblTotalOwner.Text = "Total owner : " + _DirectService.GetOwnDiplayed( GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = + _DirectService.GetOwnDiplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdOwn .Visible = true;
            }
            else
            {
                grdOwn.Visible = true;
                grdOwn1.Visible = false;
            }
        }

        grdOwn.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;


    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtOwnId.Text))
        {
            if (int.TryParse(txtOwnId.Text, out i))
            {
                and = " and ";
                str = " where Own.CodeNumber like" + "'%" + Int32.Parse(txtOwnId.Text) + "%'";
                if (txtOwnerLabel.Text != string.Empty)
                {
                    str += " " + "and" + " " + "Own.CodeNumber=" + "'" + txtOwnerLabel.Text + "'";
                    and = " and ";
                }
            }
            else
            {
                grdOwn.Visible = false;
                grdOwn1 .Visible = true;
                lblTotalOwner.Text = "Total owner : 0";
                return null;
            }
        }
        else
        {
            if (txtOwnerLabel.Text != string.Empty)
            {
                str += " " + "where" + " " + "Own.CodeNumber like" + "'%" + txtOwnerLabel.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by Own.CodeNumber";
    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;

        ClearControlSearch();
    }
    public void ClearControlSearch()
    {
        txtOwnId.Text = "";
        txtOwnerLabel.Text = ""; 
    }
    public int GetCodeNumber()
    {
        return !String.IsNullOrEmpty(Request.QueryString["codenumber"]) ? Int32.Parse(Request.QueryString["codenumber"]) : 0;
    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }
    protected void grdOwn_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        new Utility().SetSelectedColorToGridView(e, grdOwn);
    }
}
